// pages/couponlist/couponlist.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
common_Js = require("../../../component/common.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    couponstate:0,
    pageSize:10,
    pageIndex:1,
    couponList:[],
    banners:[],
    hasData:false,
    tabIndex:0,
    netWork:false,
    isLoading:true,
    couponNullText: ["暂无卡券", "暂无卡券", "暂无卡券"],
    states: [0, 1, 2],
    showDialog:false,
    showQrUrl:'',
    coupon_id:0,
  },
  handleChange({ detail}) {
    console.log('handleChange',detail)
    var that = this;
    this.data.pageIndex = 1;
    this.data.current = detail.index
    this.data.couponstate = this.data.states[detail.index]
    that.setData({
      current: detail.index,
      couponList:[],
      isLoading:true,
      tabIndex: that.data.couponstate 
    });
    that.onHttpInitUserCoupons()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onHttpInitUserCoupons();
  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInitUserCoupons: function () {
    var that = this
    fetchApi.httpPost('/product/order/coupon/list', {
      state: that.data.couponstate,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      console.log("优惠券列表数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var coupons = res.data.data.coupons;
          console.log('coupons',coupons)
          if(coupons.length > 0){
            console.log('coupons',coupons)
            for (var index in coupons) {
              that.data.couponList.splice(that.data.couponList.length, 0, coupons[index]);
            }
            that.setData({
              hasData: true,
              couponList: that.data.couponList
            })
          }
        }
      }
      that.setData({ netWork: true, isLoading: false, })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInitQrDetail: function () {
    var that = this
    fetchApi.httpPost('/product/order/coupon/detail', {
      coupon_id: that.data.coupon_id,
    }, function (res) {
      if (res.data.code == 0) {
        that.data.coupon = res.data.data.coupon;
        that.setData({
          coupon: res.data.data.coupon
        })
        
        that.data.showQrUrl = that.data.coupon.qrCode;
        that.setData({
          showQrUrl: that.data.showQrUrl,
          showDialog: true
        })
      }
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  pushToHome:function(){
    wx.switchTab({ url: '/pages/home/home', })
  },
  goDetail(e){
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: './detail/index?id=' + item.id,
    })
  },
  showQr(e){
    console.log('showQr', e)
    var item = e.currentTarget.dataset.item;
    this.data.coupon_id = item.id;
    this.onHttpInitQrDetail();
  },
  onClose(){
    this.setData({
      showDialog: false
    })
  },
  downloadQrCode() {
    var that = this;
    wx.downloadFile({
      url: that.data.showQrUrl,
      success:function(res){
        var filePath = res.tempFilePath
        wx.saveImageToPhotosAlbum({  //保存图片到相册
          filePath: filePath,
          success: function (res) {
            wx.showToast({
              title: "保存图片成功！",
              duration: 2000
            })
            that.setData({
              flag:false
            })
          },fail(res){
            console.log('保存图片 失败', res)
          }
        })
      }
    })
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      couponList:[],
      isLoading:true,
    });
    this.onHttpInitUserCoupons()
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onHttpInitUserCoupons()
  },
})